# @Time:   2021/2/26 15:31
# @Author: Zong Wei


from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class SalesGroupPage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='销售相关']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='销售小组']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    save_button = (By.ID, "lr-save")
    clear_button = (By.ID, "lr-clear")

    group_code = (By.ID, "SalesTeamCode")
    team = (By.ID, "_easyui_textbox_input6")
    team_value = (By.XPATH, "//*[@id='datagrid-row-r6-2-0']/td[@field='SalesGroupName']/div")
    sales_type = (By.ID, "SalesType")
    type_value = (By.XPATH, "//div[@id='SalesType-option']//li[2]")
    sales_name = (By.ID, "SalesName")
    leader_name = (By.ID, "TeamLeaderName")

    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='销售员']//following::tr[1]")

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def sales_group_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def add_group(self, name='测试销售小组007'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.add_button)).click()
        self.logger.info("添加销售小组")
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.group_code)).send_keys(name)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.team)).send_keys('测试销售TEAM')
        sleep(2)
        self.wait.until(ec.element_to_be_clickable(self.team_value)).click()
        sleep(1)
        self.driver.find_element_by_id("_easyui_textbox_input1").send_keys('07546')
        sleep(1)
        self.driver.find_element_by_id("datagrid-row-r1-2-0").click()

        self.driver.find_element_by_id("_easyui_textbox_input2").send_keys('07546')
        sleep(1)
        self.driver.find_element_by_id("datagrid-row-r2-2-0").click()

        self.driver.find_element_by_id("_easyui_textbox_input3").send_keys('07546')
        sleep(1)
        self.driver.find_element_by_id("datagrid-row-r3-2-0").click()

        self.driver.find_element_by_id("_easyui_textbox_input4").send_keys('07546')
        sleep(1)
        self.driver.find_element_by_id("datagrid-row-r4-2-0").click()
        self.driver.find_element_by_id("_easyui_textbox_input5").send_keys('07546')
        sleep(1)
        self.driver.find_element_by_id("datagrid-row-r5-2-0").click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.sales_type)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.type_value)).click()
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        sleep(1)
        self.logger.info("新增完成")

    def search_group(self, name='宗炜'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.sales_name)).send_keys(name)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.leader_name)).send_keys(name)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.wait.until(ec.element_to_be_clickable(self.clear_button)).click()
        self.logger.info("查询完成")

    def edit_group(self):
        self.search_group()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.group_code)).send_keys('_Ed')
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("编辑修改完成")

    def del_group(self):
        self.search_group()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.del_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("删除成功")


if __name__ == '__main__':
    pg = SalesGroupPage()
    pg.sales_group_home()
    pg.del_group()
